﻿package com.wandian.view.mediator
{ 
    import com.greensock.TweenLite;
    import com.wandian.controller.CLZNoticeName;
    import com.wandian.data.GameData;
    import com.wandian.model.InRoomPlayerDataProxy;
    import com.wandian.model.vo.PlayerVo;
    import com.wandian.view.components.ui.SoundManage;
    import com.wandian.view.components.ui.chip.AddChip;
    import com.wandian.view.components.ui.chip.CountChip;
    import com.wandian.view.components.ui.chip.CountPosition;
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.*;
	
	/**   
	 * 桌上9个玩家下注chip
	 * gamemian-chipManage_mc-{bonusPool1:{txt,mc_addChip},sitChip5:{mc_addChip，txt}}
	 * @author long
	 * @QQ 108232706
	 * @version 1.0   
	 */ 
    public class ChipMediator extends Mediator
    {
        private var arrSitChip:Array;
        private var arrbonusPool:Array;
        private var arrXY:Array;
        private var isMovePool:Boolean = false;
        private var countPosition:CountPosition;
        private var _global:GameData;
        private var _inRoomData:InRoomPlayerDataProxy;
        public static const NAME:String = "ChipMediator";

        public function ChipMediator(viewComponent:Object = null)
        {
            this.arrXY = new Array();
            this.countPosition = new CountPosition();
            this._global = GameData.getInstance();
            super(NAME, viewComponent);
            this._inRoomData = facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
            this.init();
            return;
        }

        private function init() : void
        {
            this.arrSitChip = [this.mcChip.sitChip1, this.mcChip.sitChip2, this.mcChip.sitChip3, this.mcChip.sitChip4, this.mcChip.sitChip5, this.mcChip.sitChip6, this.mcChip.sitChip7, this.mcChip.sitChip8, this.mcChip.sitChip9];
            this.arrbonusPool = [this.mcChip.bonusPool1, this.mcChip.bonusPool2, this.mcChip.bonusPool3, this.mcChip.bonusPool4, this.mcChip.bonusPool5, this.mcChip.bonusPool6, this.mcChip.bonusPool7, this.mcChip.bonusPool8];
            var seatIndex:int = 0;
			//初始化9个下注chip，与8个边池chip
            while (seatIndex < 9)
            {
                
                this.arrSitChip[seatIndex].addChip = new AddChip(this.arrSitChip[seatIndex].mc_addChip);
                if (seatIndex < 8)
                {
                    this.arrbonusPool[seatIndex].addChip = new AddChip(this.arrbonusPool[seatIndex].mc_addChip);
                }
                seatIndex = seatIndex + 1;
            }
            this.arrXY = [this.mcChip.xy1, this.mcChip.xy2, this.mcChip.xy3, this.mcChip.xy4, this.mcChip.xy5, this.mcChip.xy6, this.mcChip.xy7, this.mcChip.xy8, this.mcChip.xy9];
            this.removeAllChip();
            return;
        }

        /**
         * 根据坐位id显示下注者的金额
         * @param seatId
         * @param callChip
         */
        private function showChip(seatId:int, callChipNum:int) : void
        {
			if (callChipNum == 0)
			{
				return;
			}
            var mcChipNum:Sprite = null;
        
            this.arrSitChip[(seatId - 1)].addChip.removeChip();
            var mcCountChip:CountChip = new CountChip();
            mcCountChip.recursionHandler(callChipNum);
            var _loc_5:int = 0;
            var mcChipCount:* = mcCountChip.numChildren;
            if (mcChipCount == 1)
            {
                SoundManage.playSound("CallChip1");
            }
            else if (mcChipCount < 5)
            {
                SoundManage.playSound("CallChip3");
            }
            else if (mcChipCount < 8)
            {
                SoundManage.playSound("CallChip2");
            }
            else
            {
                SoundManage.playSound("CallChip4");
            }
            var _loc_7:* = mcCountChip.numChildren;
            while (mcCountChip.numChildren)
            {
                
                mcChipNum = mcCountChip.removeChildAt((mcCountChip.numChildren - 1)) as Sprite;
                if (mcChipNum != null)
                {
                    _loc_5 = _loc_5 + 1;
                    this.mcChip.addChild(mcChipNum);
                    mcChipNum.x = this.arrXY[(seatId - 1)].x;
                    mcChipNum.y = this.arrXY[(seatId - 1)].y;
                    mcChipNum.visible = true;
                    TweenLite.to(mcChipNum, 0.4, {x:this.arrSitChip[(seatId - 1)].x, y:this.arrSitChip[(seatId - 1)].y, onComplete:this.moveToSitPotFinish, onCompleteParams:[mcChipNum, seatId, callChipNum, _loc_5, mcCountChip.numChildren == 0]});
                    mcChipNum = null;
                }
            }
            return;
        }

        private function moveToSitPotFinish(mcChipNum:Sprite, seatId:int, callChipNum:int, param4:int, param5:Boolean) : void
        {
            if (this.mcChip.contains(mcChipNum))
            {
                this.mcChip.removeChild(mcChipNum);
                mcChipNum = null;
            }
            if (param5)
            {
                this.arrSitChip[(seatId - 1)].addChip.removeChip();
                this.arrSitChip[(seatId - 1)].addChip.showChip(callChipNum);
                this.arrSitChip[(seatId - 1)].addChip.showTxtBg(callChipNum, false);
            }
            return;
        }

        private function showPoolChip(seatId:int, param2:int) : void
        {
            if (param2 == 0)
            {
                return;
            }
            this.arrbonusPool[seatId].addChip.removeChip();
            this.arrbonusPool[seatId].addChip.showChip(param2);
            this.arrbonusPool[seatId].addChip.showTxtBg(param2, true);
            return;
        }

        public function moveChipToPool(param1:Array) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Sprite = null;
            this.isMovePool = false;
            if (param1.length <= 0)
            {
                return;
            }
            SoundManage.playSound("MoveChip");
            var seatIndex:int = 0;
            while (seatIndex < this.arrSitChip.length)
            {
                
                this.arrSitChip[seatIndex].addChip.hideTxtBg();
                if (this.arrSitChip[seatIndex].addChip.countChip.numChildren > 0)
                {
					//桌上钱清除，
                    this.arrSitChip[seatIndex].txt.text = "";
                    _loc_3 = param1[0][0];
                    _loc_4 = 0;
                    _loc_5 = this.arrSitChip[seatIndex].addChip.countChip.numChildren;
                    _loc_6 = _loc_5;
                    while (this.arrSitChip[seatIndex].addChip.countChip.numChildren)
                    {
                        
                        _loc_7 = this.arrSitChip[seatIndex].addChip.countChip.removeChildAt((this.arrSitChip[seatIndex].addChip.countChip.numChildren - 1)) as Sprite;
                        _loc_5 = _loc_5 - 1;
                        if (_loc_7 != null)
                        {
                            _loc_4 = _loc_4 + 1;
                            this.mcChip.addChild(_loc_7);
                            _loc_7.x = this.arrSitChip[seatIndex].x;
                            _loc_7.y = this.arrSitChip[seatIndex].y;
                            TweenLite.to(_loc_7, 1 - 1 / _loc_6 * _loc_5, {x:this.arrbonusPool[_loc_3].x + this.countPosition.countXY(_loc_4).x, y:this.arrbonusPool[_loc_3].y + this.countPosition.countXY(_loc_4).y, onComplete:this.moveToPoolFinish, onCompleteParams:[_loc_7, param1, seatIndex, _loc_4, _loc_5]});
                            _loc_7 = null;
                        }
                    }
                }
                seatIndex = seatIndex + 1;
            }
            return;
        }

        private function moveToPoolFinish(param1:Sprite, param2:Array, param3:int, param4:int, param5:int) : void
        {
            if (this.mcChip.contains(param1))
            {
                this.mcChip.removeChild(param1);
                param1 = null;
            }
            if (param5 == 0)
            {
                this.hideSitPoolHandler(param2);
            }
            return;
        }

        private function hideSitPoolHandler(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (!this.isMovePool)
            {
                this.isMovePool = true;
                _loc_2 = 0;
                while (_loc_2 < param1.length)
                {
                    
                    _loc_3 = param1[_loc_2][0];
                    this.arrbonusPool[_loc_3].addChip.removeChip();
                    this.arrbonusPool[_loc_3].addChip.showChip(param1[_loc_2][1]);
                    this.arrbonusPool[_loc_3].addChip.showTxtBg(param1[_loc_2][1], true);
                    this.arrbonusPool[_loc_3].addChip.countChip.alpha = 0;
                    TweenLite.to(this.arrbonusPool[_loc_3].addChip.countChip, 0.2, {alpha:1, onComplete:this.showSitPoolHandler, onCompleteParams:[param1[_loc_2][1], _loc_3]});
                    _loc_2 = _loc_2 + 1;
                }
            }
            return;
        }

        private function showSitPoolHandler(param1:int, param2:int) : void
        {
            this.arrbonusPool[param2].addChip.showTxtBg(param1, true);
            return;
        }

        /**
         * 将边池的金钱给赢家,同时给庄家抽成
         * @param potId
         * @param arrWinPlayer
         */
        public function moveChipToSit(potId:int, arrWinPlayer:Array) : void
        {
            var mcCountChip:CountChip = null;
            var reChildNum:int = 0;
            var childNum:int = 0;
            var totalChildNum:int = 0;
            var mcChipNum:Sprite = null;
            this.arrbonusPool[potId].addChip.removeChip();
            SoundManage.playSound("MoveChip");
            var winIndex:int = 0;
            while (winIndex < arrWinPlayer.length)
            {
                
                mcCountChip = new CountChip();
                this.mcChip.addChild(mcCountChip);
                mcCountChip.x = this.arrbonusPool[potId].x;
                mcCountChip.y = this.arrbonusPool[potId].y;
                mcCountChip.recursionHandler(arrWinPlayer[winIndex].potMoney);
                this.sendNotification(CLZNoticeName.SHOW_FLASH_LEVEL_TO_SEAT_ID, arrWinPlayer[winIndex].seatId);
                reChildNum = 0;
                childNum = mcCountChip.numChildren;
                totalChildNum = childNum;
                while (mcCountChip.numChildren)
                {
                    
                    childNum = childNum - 1;
                    mcChipNum = mcCountChip.removeChildAt((mcCountChip.numChildren - 1)) as Sprite;
                    if (mcChipNum != null)
                    {
                        reChildNum = reChildNum + 1;
                        this.mcChip.addChild(mcChipNum);
                        mcChipNum.x = mcCountChip.x;
                        mcChipNum.y = mcCountChip.y;
                        TweenLite.to(mcChipNum, 1 - 1 / totalChildNum * childNum, {x:this.arrXY[(arrWinPlayer[winIndex].seatId - 1)].x, y:this.arrXY[(arrWinPlayer[winIndex].seatId - 1)].y, onComplete:this.moveToSitFinish, onCompleteParams:[mcChipNum, arrWinPlayer[winIndex], childNum == 0]});
                        mcChipNum = null;
                    }
                }
                winIndex = winIndex + 1;
            }
            return;
        }

        private function moveToSitFinish(mcChipNum:Sprite, winData:Object, param3:Boolean) : void
        {
            var playerVo:PlayerVo = null;
            if (param3)
            {
                SoundManage.playSound("EarnMoney");
                if (winData.seatId == 0)
                {
                    return;
                }
                playerVo = this._inRoomData.getSitPlayerBySeatId(winData.seatId);
                if (playerVo != null)
                {
                }
                if (playerVo.state == GameData.PLAYING_GAME)
                {
                    playerVo.stake = winData.stake;
                    this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, playerVo);
                }
            }
            if (mcChipNum != null)
            {
				if (this.mcChip.contains(mcChipNum))
				{
					this.mcChip.removeChild(mcChipNum);
					mcChipNum = null;
				}
            }
          
            return;
        }

        public function removeAllChip() : void
        {
            var k:int;
            var i:int;
            var j:int;
            try
            {
                k;
                while (k < this.arrbonusPool.length)
                {
                    
                    TweenLite.killTweensOf(this.arrbonusPool[k], false);
                    k = (k + 1);
                }
                i;
                while (i < this.arrSitChip.length)
                {
                    
                    this.arrSitChip[i].addChip.removeChip();
                    i = (i + 1);
                }
                j;
                while (j < this.arrbonusPool.length)
                {
                    
                    this.arrbonusPool[j].addChip.removeChip();
                    j = (j + 1);
                }
            }
            catch (err:Error)
            {
            }
            return;
        }

        public function resetChip() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this.arrSitChip.length)
            {
                
                this.arrSitChip[_loc_1].addChip.hideTxtBg();
                _loc_1 = _loc_1 + 1;
            }
            return;
        }

        public function getChip(param1:int) : int
        {
            return this.arrSitChip[(param1 - 1)].addChip.nChip;
        }

        public function sitToSitMoney(fromPlayerVo:PlayerVo, toPlayerVo:PlayerVo, chipMoney:uint) : void
        {
            var fromSeatId:* = fromPlayerVo.seatId;
            var toSeatId:* = toPlayerVo.seatId;
            if (fromSeatId >= 1)
            {
            }
            if (toSeatId >= 1)
            {
            }
            if (fromSeatId <= 9)
            {
            }
            if (toSeatId > 9)
            {
                return;
            }
            var mcBonusPoolClz:* = GameData.getClass("BonusPool");
            var mcBonusPool:* = new mcBonusPoolClz;
            mcBonusPool["addChip"] = new AddChip(mcBonusPool["mc_addChip"]);
            mcBonusPool["addChip"].showChip(chipMoney);
            mcBonusPool["addChip"].showTxtBg(chipMoney, true);
            mcBonusPool.x = this.arrXY[(fromSeatId - 1)].x;
            mcBonusPool.y = this.arrXY[(fromSeatId - 1)].y;
            this.mcChip.addChild(mcBonusPool);
            TweenLite.to(mcBonusPool, 2, {x:this.arrXY[(toSeatId - 1)].x, y:this.arrXY[(toSeatId - 1)].y, onComplete:this.sitToSitFinish, onCompleteParams:[mcBonusPool, fromPlayerVo, toPlayerVo, chipMoney]});
            return;
        }

        private function sitToSitFinish(param1:Sprite, param2:PlayerVo, param3:PlayerVo, param4:int) : void
        {
            param3.giveChip = param3.giveChip + param4;
            this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, param3);
            var _loc_5:* = param1;
            if (_loc_5 != null)
            {
            }
            if (this.mcChip.contains(_loc_5))
            {
                this.mcChip.removeChild(_loc_5);
                _loc_5 = null;
            }
            return;
        }

        override public function onRegister() : void
        {
            return;
        }

        override public function listNotificationInterests() : Array
        {
            return [CLZNoticeName.BONUS_POOL_SHOW, CLZNoticeName.START_GAME, CLZNoticeName.OPERATION_SUCC, CLZNoticeName.CUR_POT, CLZNoticeName.POTS, CLZNoticeName.GAME_OVER, CLZNoticeName.GIVE_CHIP_RES, CLZNoticeName.SITDOWN_RSP, CLZNoticeName.OUT_GAME_ROOM_REMOVE];
        }

        override public function handleNotification(notification:INotification) : void
        {
            var _loc_2:Array = null;
            var data:Object = null;
            var fromPlayerVo:PlayerVo = null;
            var toPlayerVo:PlayerVo = null;
            var sdownPlayerVo:PlayerVo = null;
            var playerIndex:int = 0;
            var playerData:Object = null;
            var plyerVo:PlayerVo = null;
            switch(notification.getName())
            {
                case CLZNoticeName.BONUS_POOL_SHOW:
                {
                    _loc_2 = notification.getBody() as Array;
                    playerIndex = 0;
                    while (playerIndex < _loc_2.length)
                    {
                        
                        this.showPoolChip(playerIndex, _loc_2[playerIndex]);
                        playerIndex = playerIndex + 1;
                    }
                    break;
                }
                case CLZNoticeName.START_GAME:
                {
                    data = notification.getBody();
                    playerIndex = 0;
                    while (playerIndex < data.arrPlayer.length)
                    {
                        
                        playerData = data.arrPlayer[playerIndex];
                        plyerVo = this._inRoomData.getSitPlayerBySeatId(playerData.seatId);
                        if (playerData.callMoney > plyerVo.stake)
                        {
                            playerData.callMoney = plyerVo.stake;
                        }
                        this.showChip(playerData.seatId, playerData.callMoney);
                        plyerVo.callChip = playerData.callMoney;
                        plyerVo.stake = plyerVo.stake - playerData.callMoney;
                        plyerVo.lastAction = playerData.callType == 0 ? (0) : (playerData.callType + 5);
                        plyerVo.state = GameData.PLAYING_GAME;
                        this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, plyerVo);
                        this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, plyerVo);
                        playerIndex = playerIndex + 1;
                    }
                    break;
                }
                case CLZNoticeName.OPERATION_SUCC:
                {
                    data = notification.getBody();
                    this.showChip(data.seatId, data.callChip);
                    break;
                }
                case CLZNoticeName.CUR_POT:
                {
                    this.moveChipToPool(notification.getBody() as Array);
                    break;
                }
                case CLZNoticeName.POTS:
                {
                    data = notification.getBody();
                    this.moveChipToSit(data.potId, data.arrWinPlayer);
                    break;
                }
                case CLZNoticeName.GAME_OVER:
                {
                    this.initRemoveChip();
                    break;
                }
                case CLZNoticeName.GIVE_CHIP_RES:
                {
                    data = notification.getBody();
                    fromPlayerVo = this._inRoomData.getSitPlayerByMid(data.fromMid);
                    toPlayerVo = this._inRoomData.getSitPlayerByMid(data.toMid);
                    if (fromPlayerVo != null)
                    {
                    }
                    if (toPlayerVo == null)
                    {
                        return;
                    }
                    toPlayerVo.isSend = true;
                    fromPlayerVo.stake = fromPlayerVo.stake - data.chip;//下了chip金额
                    this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, fromPlayerVo);
                    this.sitToSitMoney(fromPlayerVo, toPlayerVo, data.chip);
                    break;
                }
                case CLZNoticeName.SITDOWN_RSP:
                {
                    sdownPlayerVo = notification.getBody() as PlayerVo;
                    if (sdownPlayerVo.callChip > 0)//游戏刚坐下一般是0
                    {
                        this.initShowSitChip(sdownPlayerVo.seatId, sdownPlayerVo.callChip);
                    }
                    break;
                }
                case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
                {
                    this.initRemoveChip();
                    GameData.removeNumChildre(this.mcChip, true);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function initShowSitChip(seatId:int, callChip:uint) : void
        {
            this.arrSitChip[(seatId - 1)].addChip.removeChip();
            this.arrSitChip[(seatId - 1)].addChip.showChip(callChip);
            this.arrSitChip[(seatId - 1)].addChip.showTxtBg(callChip, false);
            return;
        }

        /**
         * 将坐位与边池的chip移除
         */
        private function initRemoveChip() : void
        {
            var seatIndex:int = 0;
            while (seatIndex < this.arrSitChip.length)
            {
                
                this.arrSitChip[seatIndex].addChip.removeChip();
                if (seatIndex < 8)
                {
                    this.arrbonusPool[seatIndex].addChip.removeChip();
                }
                seatIndex = seatIndex + 1;
            }
            return;
        }

        private function get mcChip() : MovieClip
        {
            return viewComponent as MovieClip;
        }

    }
}
